package www.ld.com.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ld.com.domain.attr.pojo.AttrPO;
import www.ld.com.domain.attr.query.AttrQuery;
import www.ld.com.domain.attr.vo.AttrVO;
import www.ld.com.mapper.AttrMapper;

import javax.annotation.Resource;

@Service
public class AttrService {
    @Resource
    private AttrMapper attrMapper;
    @Resource
    private CategoryCacheService categoryCachService;


    public IPage<AttrVO> pageAttrByAttrQuery(AttrQuery attrQuery) {
        IPage<AttrVO> attrVOIPage = attrMapper.pageAttrByAttrQuery(new Page<>(attrQuery.getCurrent(), attrQuery.getSize()), attrQuery);
        attrVOIPage.getRecords().forEach(v -> {
            v.setCategoryName(categoryCachService.getCategoryNameById(v.getCategoryId()));
        });
        return attrVOIPage;
    }

    public AttrPO getAttrPOById(String id) {
        return attrMapper.selectById(id);
    }

    @Transactional
    public void saveAttrPO(AttrPO attrPO) {
        attrMapper.insert(attrPO);
    }

    @Transactional
    public void updateAttrPO(AttrPO attrPO) {
        attrMapper.updateById(attrPO);
    }

    @Transactional
    public void deleteAttrById(String id) {
        //TODO 判读属性分组中是否有该属性
        attrMapper.deleteById(id);
    }
}
